import { defHttp } from '/@/utils/http/axios';
import type {
  EventDefinition,
  Event,
  EventFilter,
  EventQuery,
  EventStatistics,
  EventTrigger,
  EventSubscription,
  EventTemplate,
  EventProcessingResult,
} from './event-types';

enum Api {
  // 事件定义管理
  EVENT_DEFINITIONS = '/resource-center/events/definitions',
  EVENT_DEFINITION = '/resource-center/events/definitions/:id',

  // 事件实例管理
  EVENTS = '/resource-center/events',
  EVENT = '/resource-center/events/:id',
  TRIGGER_EVENT = '/resource-center/events/trigger',
  PROCESS_EVENT = '/resource-center/events/:id/process',

  // 事件过滤器管理
  EVENT_FILTERS = '/resource-center/events/filters',
  EVENT_FILTER = '/resource-center/events/filters/:id',

  // 事件触发器管理
  EVENT_TRIGGERS = '/resource-center/events/triggers',
  EVENT_TRIGGER = '/resource-center/events/triggers/:id',

  // 事件订阅管理
  EVENT_SUBSCRIPTIONS = '/resource-center/events/subscriptions',
  EVENT_SUBSCRIPTION = '/resource-center/events/subscriptions/:id',

  // 事件模板管理
  EVENT_TEMPLATES = '/resource-center/events/templates',
  EVENT_TEMPLATE = '/resource-center/events/templates/:id',

  // 事件统计和监控
  EVENT_STATISTICS = '/resource-center/events/statistics',
  EVENT_PROCESSING_RESULTS = '/resource-center/events/processing-results',

  // 资源相关事件
  RESOURCE_EVENTS = '/resource-center/resources/:resourceId/events',
  RESOURCE_EVENT_STATISTICS = '/resource-center/resources/:resourceId/events/statistics',
}

// ==================== 事件定义管理 ====================

/**
 * 获取事件定义列表
 */
export function getEventDefinitions(params?: any) {
  return defHttp.get<EventDefinition[]>({
    url: Api.EVENT_DEFINITIONS,
    params,
  });
}

/**
 * 获取事件定义详情
 */
export function getEventDefinition(id: string) {
  return defHttp.get<EventDefinition>({
    url: Api.EVENT_DEFINITION.replace(':id', id),
  });
}

/**
 * 创建事件定义
 */
export function createEventDefinition(data: Partial<EventDefinition>) {
  return defHttp.post<EventDefinition>({
    url: Api.EVENT_DEFINITIONS,
    data,
  });
}

/**
 * 更新事件定义
 */
export function updateEventDefinition(
  id: string,
  data: Partial<EventDefinition>,
) {
  return defHttp.put<EventDefinition>({
    url: Api.EVENT_DEFINITION.replace(':id', id),
    data,
  });
}

/**
 * 删除事件定义
 */
export function deleteEventDefinition(id: string) {
  return defHttp.delete<void>({
    url: Api.EVENT_DEFINITION.replace(':id', id),
  });
}

/**
 * 启用/禁用事件定义
 */
export function toggleEventDefinition(id: string, enabled: boolean) {
  return defHttp.patch<void>({
    url: Api.EVENT_DEFINITION.replace(':id', id),
    data: { enabled },
  });
}

// ==================== 事件实例管理 ====================

/**
 * 获取事件列表
 */
export function getEvents(params?: EventQuery) {
  return defHttp.get<Event[]>({
    url: Api.EVENTS,
    params,
  });
}

/**
 * 获取事件详情
 */
export function getEvent(id: string) {
  return defHttp.get<Event>({
    url: Api.EVENT.replace(':id', id),
  });
}

/**
 * 触发事件
 */
export function triggerEvent(data: {
  type: string;
  resourceId: string;
  resourceType: string;
  payload?: Record<string, any>;
  metadata?: Record<string, any>;
  tags?: string[];
}) {
  return defHttp.post<Event>({
    url: Api.TRIGGER_EVENT,
    data,
  });
}

/**
 * 处理事件
 */
export function processEvent(id: string) {
  return defHttp.post<EventProcessingResult>({
    url: Api.PROCESS_EVENT.replace(':id', id),
  });
}

/**
 * 批量处理事件
 */
export function batchProcessEvents(eventIds: string[]) {
  return defHttp.post<EventProcessingResult[]>({
    url: `${Api.EVENTS}/batch-process`,
    data: { eventIds },
  });
}

// ==================== 事件过滤器管理 ====================

/**
 * 获取事件过滤器列表
 */
export function getEventFilters(params?: any) {
  return defHttp.get<EventFilter[]>({
    url: Api.EVENT_FILTERS,
    params,
  });
}

/**
 * 获取事件过滤器详情
 */
export function getEventFilter(id: string) {
  return defHttp.get<EventFilter>({
    url: Api.EVENT_FILTER.replace(':id', id),
  });
}

/**
 * 创建事件过滤器
 */
export function createEventFilter(data: Partial<EventFilter>) {
  return defHttp.post<EventFilter>({
    url: Api.EVENT_FILTERS,
    data,
  });
}

/**
 * 更新事件过滤器
 */
export function updateEventFilter(id: string, data: Partial<EventFilter>) {
  return defHttp.put<EventFilter>({
    url: Api.EVENT_FILTER.replace(':id', id),
    data,
  });
}

/**
 * 删除事件过滤器
 */
export function deleteEventFilter(id: string) {
  return defHttp.delete<void>({
    url: Api.EVENT_FILTER.replace(':id', id),
  });
}

/**
 * 启用/禁用事件过滤器
 */
export function toggleEventFilter(id: string, enabled: boolean) {
  return defHttp.patch<void>({
    url: Api.EVENT_FILTER.replace(':id', id),
    data: { enabled },
  });
}

// ==================== 事件触发器管理 ====================

/**
 * 获取事件触发器列表
 */
export function getEventTriggers(params?: any) {
  return defHttp.get<EventTrigger[]>({
    url: Api.EVENT_TRIGGERS,
    params,
  });
}

/**
 * 获取事件触发器详情
 */
export function getEventTrigger(id: string) {
  return defHttp.get<EventTrigger>({
    url: Api.EVENT_TRIGGER.replace(':id', id),
  });
}

/**
 * 创建事件触发器
 */
export function createEventTrigger(data: Partial<EventTrigger>) {
  return defHttp.post<EventTrigger>({
    url: Api.EVENT_TRIGGERS,
    data,
  });
}

/**
 * 更新事件触发器
 */
export function updateEventTrigger(id: string, data: Partial<EventTrigger>) {
  return defHttp.put<EventTrigger>({
    url: Api.EVENT_TRIGGER.replace(':id', id),
    data,
  });
}

/**
 * 删除事件触发器
 */
export function deleteEventTrigger(id: string) {
  return defHttp.delete<void>({
    url: Api.EVENT_TRIGGER.replace(':id', id),
  });
}

/**
 * 启用/禁用事件触发器
 */
export function toggleEventTrigger(id: string, enabled: boolean) {
  return defHttp.patch<void>({
    url: Api.EVENT_TRIGGER.replace(':id', id),
    data: { enabled },
  });
}

// ==================== 事件订阅管理 ====================

/**
 * 获取事件订阅列表
 */
export function getEventSubscriptions(params?: any) {
  return defHttp.get<EventSubscription[]>({
    url: Api.EVENT_SUBSCRIPTIONS,
    params,
  });
}

/**
 * 获取事件订阅详情
 */
export function getEventSubscription(id: string) {
  return defHttp.get<EventSubscription>({
    url: Api.EVENT_SUBSCRIPTION.replace(':id', id),
  });
}

/**
 * 创建事件订阅
 */
export function createEventSubscription(data: Partial<EventSubscription>) {
  return defHttp.post<EventSubscription>({
    url: Api.EVENT_SUBSCRIPTIONS,
    data,
  });
}

/**
 * 更新事件订阅
 */
export function updateEventSubscription(
  id: string,
  data: Partial<EventSubscription>,
) {
  return defHttp.put<EventSubscription>({
    url: Api.EVENT_SUBSCRIPTION.replace(':id', id),
    data,
  });
}

/**
 * 删除事件订阅
 */
export function deleteEventSubscription(id: string) {
  return defHttp.delete<void>({
    url: Api.EVENT_SUBSCRIPTION.replace(':id', id),
  });
}

/**
 * 启用/禁用事件订阅
 */
export function toggleEventSubscription(id: string, enabled: boolean) {
  return defHttp.patch<void>({
    url: Api.EVENT_SUBSCRIPTION.replace(':id', id),
    data: { enabled },
  });
}

// ==================== 事件模板管理 ====================

/**
 * 获取事件模板列表
 */
export function getEventTemplates(params?: any) {
  return defHttp.get<EventTemplate[]>({
    url: Api.EVENT_TEMPLATES,
    params,
  });
}

/**
 * 获取事件模板详情
 */
export function getEventTemplate(id: string) {
  return defHttp.get<EventTemplate>({
    url: Api.EVENT_TEMPLATE.replace(':id', id),
  });
}

/**
 * 创建事件模板
 */
export function createEventTemplate(data: Partial<EventTemplate>) {
  return defHttp.post<EventTemplate>({
    url: Api.EVENT_TEMPLATES,
    data,
  });
}

/**
 * 更新事件模板
 */
export function updateEventTemplate(id: string, data: Partial<EventTemplate>) {
  return defHttp.put<EventTemplate>({
    url: Api.EVENT_TEMPLATE.replace(':id', id),
    data,
  });
}

/**
 * 删除事件模板
 */
export function deleteEventTemplate(id: string) {
  return defHttp.delete<void>({
    url: Api.EVENT_TEMPLATE.replace(':id', id),
  });
}

/**
 * 启用/禁用事件模板
 */
export function toggleEventTemplate(id: string, enabled: boolean) {
  return defHttp.patch<void>({
    url: Api.EVENT_TEMPLATE.replace(':id', id),
    data: { enabled },
  });
}

// ==================== 事件统计和监控 ====================

/**
 * 获取事件统计信息
 */
export function getEventStatistics(params?: any) {
  return defHttp.get<EventStatistics>({
    url: Api.EVENT_STATISTICS,
    params,
  });
}

/**
 * 获取事件处理结果
 */
export function getEventProcessingResults(params?: any) {
  return defHttp.get<EventProcessingResult[]>({
    url: Api.EVENT_PROCESSING_RESULTS,
    params,
  });
}

/**
 * 获取事件处理结果详情
 */
export function getEventProcessingResult(eventId: string) {
  return defHttp.get<EventProcessingResult>({
    url: `${Api.EVENT_PROCESSING_RESULTS}/${eventId}`,
  });
}

// ==================== 资源相关事件 ====================

/**
 * 获取资源的事件列表
 */
export function getResourceEvents(resourceId: string, params?: EventQuery) {
  return defHttp.get<Event[]>({
    url: Api.RESOURCE_EVENTS.replace(':resourceId', resourceId),
    params,
  });
}

/**
 * 获取资源的事件统计信息
 */
export function getResourceEventStatistics(resourceId: string) {
  return defHttp.get<EventStatistics>({
    url: Api.RESOURCE_EVENT_STATISTICS.replace(':resourceId', resourceId),
  });
}

/**
 * 为资源触发事件
 */
export function triggerResourceEvent(
  resourceId: string,
  data: {
    type: string;
    payload?: Record<string, any>;
    metadata?: Record<string, any>;
    tags?: string[];
  },
) {
  return defHttp.post<Event>({
    url: `${Api.RESOURCE_EVENTS.replace(':resourceId', resourceId)}/trigger`,
    data,
  });
}
